package io.gitee.zicai.task.job

import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Future

/**
 * JobHolder
 * @author zicai
 * @since 2020-05-18
 */
object JobHolder {

    private val holder: ConcurrentHashMap<String, Future<*>?> = ConcurrentHashMap()

    fun makeJob(block: () -> Unit) = Runnable { block() }

    fun getJob(jobName: String) = holder[jobName]

    fun add(jobName: String, job: Future<*>?) {
        holder[jobName] = job
    }

    fun remove(jobName: String) {
        holder.remove(jobName)?.cancel(false)
    }

    fun isStart(jobName: String): Boolean {
        return jobName.isNotBlank() && getJob(jobName) != null
    }
}